package com.vclee.fast.utils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public final static String DATE_DAY_PATTERN = "yyyy-MM-dd";


    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static LocalDate formatDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime formatDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date parseDate(LocalDate localDate) {
        return Date.from(localDate.atTime(LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC));
    }

    public static Date parseDateTime(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.UTC));
    }

    public static Date parseStr(String strDate,String pattern) {
        try {
            if (strDate != null) {
                SimpleDateFormat df = new SimpleDateFormat(pattern);
                return df.parse(strDate);
            }
        }catch (Exception ignored){
        }

        return null;
    }

    /**
     * 获取同步节点时间
     */
    public static long getSyncNodeTime() {
//        try {
//            URL tbLink = new URL("http://www.taobao.com");
//            URLConnection uc = url.openConnection();// 生成连接对象
//            uc.connect();// 发出连接
//            return uc.getDate();
//        } catch (IOException e) {
//            return 0;
//        }
        return System.currentTimeMillis();
    }

    /**
     * 获取对应日期的月份天数
     */
    public static int getMonthDay(Date date) {
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }


    public static Date handPlusTime(Date date, List<Integer> arr, Integer timeType) {
        LocalDateTime dateTime = formatDateTime(date);
        if(arr!=null){
           if(arr.size()==1){
               dateTime = dateTime.plusYears(arr.get(0));
           }else if(arr.size()==2){
               dateTime = dateTime.plusYears(arr.get(0));
               dateTime = dateTime.plusMonths(arr.get(1));
           }else if(arr.size()==3){
               dateTime = dateTime.plusYears(arr.get(0));
               dateTime = dateTime.plusMonths(arr.get(1));
               dateTime = dateTime.plusDays(arr.get(2));
           }
        }
        Date newDate = parseDateTime(dateTime);
        if(timeType == 1) return newDate;
        if(timeType == 2) return parseStr(DateUtils.format(newDate,DATE_DAY_PATTERN)+" 00:00:00",DATE_TIME_PATTERN);
        return parseStr(DateUtils.format(newDate,DATE_DAY_PATTERN)+" 23:59:59",DATE_TIME_PATTERN);
    }

}
